home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
EnigmA Amiga Run 1996 February
/
EnigmA AMIGA RUN 04 (1996)(G.R. Edizioni)(IT)[!][issue 1996-02][Skylink CD III].iso
/
earcd
/
comm2
/
kms20src.lha
/
KMSC
/
kmscookie.c
< prev
next >
Wrap
C/C++ Source or Header
|
1994-02-13
|
4KB
|
168 lines
/**
** KMSCookie.c
**
** Prints a random fortune out of a database.
**
** © Copyright 1993 Thomas Schwarz, Germany
** All Rights Reserved.
**/
// Includes
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <clib/exec_protos.h>
#include <clib/dos_protos.h>
#include <dos/dos.h>
#include <exec/types.h>
// Defines
#define USAGE "KMSCookie 1.0 -*- KMS Fortune Cookie Server ;-) (c)1993 Thomas Schwarz\n" \
"Randomly displays a fortune cookie from a chosen database.\n" \
"(Based on \"PFortune\" by Peter Simons)\n\n" \
"USAGE: %s <database>\n\n" \
"Default for <database> is \"KMS_Cookie.Dat\" in the current directory.\n\n" \
"Format of database:\n" \
" Cookie 1 Line 1\n" \
" Cookie 1 Line 2\n" \
" ...\n" \
" <empty line>\n" \
" <empty line>\n" \
" Cookie 2\n" \
" ...\n"
#define BUFFERSIZE 256
#define MARGIN 80
STRPTR Version = "\0$VER: KMSCookie 1.0 ("__COMMODORE_DATE__")";
// Prototypes
int MyBreak(VOID);
void OutCentered(int, char *);
void MyExit(int, BPTR);
int InLine(char *, BPTR);
int MyBreak(VOID)
{
return 0;
}
/**************************************************************************/
int main(int argc, char *argv[])
{
char *path = "KMS_Cookie.Dat", officialrule[BUFFERSIZE];
int next, character, firstempty;
unsigned long length, randpos;
BPTR fp;
onbreak(MyBreak);
if((argc == 2 && !strcmp("?", argv[1])) || argc > 2)
{
printf(USAGE, argv[0]);
MyExit(0, 0);
}
if(argc == 2)
path = argv[1];
if(!(fp = Open(path, MODE_OLDFILE)))
{
printf("Can't open %s\n\n", path);
printf(USAGE, argv[0]);
MyExit(5, fp);
}
Seek(fp, 0L, OFFSET_END);
length = Seek(fp, 0L, OFFSET_CURRENT);
while(TRUE)
{
randpos = (unsigned long)clock() % length - 1;
Seek(fp, randpos, OFFSET_BEGINNING);
while((character = FGetC(fp)) != EOF)
{
if(character == '\n')
if((character = FGetC(fp)) != EOF && character == '\n')
if((character = FGetC(fp)) != EOF && character == '\n')
break;
}
if(!(InLine(officialrule, fp)))
continue;
else
{
if(*officialrule == ';')
continue;
printf("\n");
OutCentered(MARGIN, officialrule);
}
next = 0;
firstempty = FALSE;
while(TRUE)
{
if(!(InLine(officialrule, fp)))
MyExit(0, fp);
else
{
if(*officialrule == '\n' && firstempty)
MyExit(0, fp);
else
firstempty = TRUE;
OutCentered(MARGIN, officialrule);
if(next++ >= 10)
MyExit(0, fp);
}
continue;
}
}
}
void MyExit(int num, BPTR fp)
{
if(fp)
Close(fp);
printf("\n");
exit(num);
}
void OutCentered(int number, char *rule)
{
int offset;
char fortune[BUFFERSIZE];
strcpy(fortune, rule);
offset = strlen(fortune);
for(offset = ((number - offset) / 2); offset > 0; offset--)
strins(fortune, " ");
printf("%s", fortune);
}
int InLine(char *string, BPTR fp)
{
int c;
do
{
if((c = FGetC(fp)) == EOF)
return 0;
*string++ = c;
} while(c != '\n');
*string = '\0';
return 1;
}